use super::{json, Value};
use std::fmt::Debug;

pub enum ECode {
    Null,
    MissKey,
    MissOrEmpty,
    Para,
    Time,
    JsonFormat,
    FileDel,
    FileRead,
    FileWrite,
    DirMake,
    Post,
    PostText,
    PostJson,
    EmptyData,
    NoServe,
    Task,
    Tech,
    Psn,
    Code,
    Custom,
    BackUp,
    Collect,
    Db,
    Cache,
    User,
    Connect,
    ReadVal,
    SendVal,
    Format,
}

pub fn err_value<T: Debug>(v: ECode, e: T) -> Value {
    let (code, info) = match v {
        ECode::Null => (2000, ""),
        ECode::MissKey => (2001, "miss"),
        ECode::MissOrEmpty => (2002, "miss or empty"),
        ECode::Para => (2003, "para"),
        ECode::Time => (2004, "time format"),
        ECode::JsonFormat => (2100, "json format"),
        ECode::FileDel => (2200, "rm file"),
        ECode::FileRead => (2201, "read file"),
        ECode::FileWrite => (2202, "write file"),
        ECode::DirMake => (2300, "make dir"),
        ECode::Post => (2400, "post"),
        ECode::PostText => (2401, "post text"),
        ECode::PostJson => (2402, "post json"),
        ECode::EmptyData => (2500, "data empty"),
        ECode::NoServe => (2501, "no serve"),
        ECode::Task => (2502, "task"),
        ECode::Tech => (2503, "tech"),
        ECode::Psn => (2504, "psn"),
        ECode::Code => (2505, "code"),
        ECode::Custom => (2506, "code custom"),
        ECode::BackUp => (2507, "backup"),
        ECode::Collect => (2508, "collect"),
        ECode::Db => (2600, "rocksdb"),
        ECode::Cache => (2700, "cache"),
        ECode::User => (2800, "user"),
        ECode::Connect => (2900, "connect"),
        ECode::ReadVal => (2901, "read value"),
        ECode::SendVal => (2902, "send value"),
        ECode::Format => (2903, "format"),
    };

    if info.is_empty() {
        json!({
            "code": code,
            "success": false,
            "fail": true,
        })
    } else {
        json!({
            "code": code,
            "success": false,
            "fail": true,
            "message": format!("{info} error: {:?}", e),
        })
    }
}
